package com.education.cms.service;

import com.education.cms.dao.CourseDao;
import com.education.cms.dao.UserCourseDao;
import com.education.cms.domain.Course;
import com.education.cms.domain.UserCourse;
import com.education.cms.dto.course.AddCourseDto;
import com.education.cms.dto.course.EditCourseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CourseService {
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private UserCourseDao userCourseDao;
    /**
     * 课程列表
     */
    public List<Course> getCourseList(){
        return courseDao.findAll();
    }
    /**
     * 根据courseId得到选课(进入修改页面回显
     */
    public Course course(int courseId){
        return courseDao.findCourseByCourseId(courseId);
    }
    /**
     * 修改一门选课
     */
    public String editCourse(EditCourseDto editCourseDto){
        Course course = courseDao.findCourseByCourseId(editCourseDto.getCourseId());
        course.setCourseName(editCourseDto.getCourseName());
        course.setCourseTime(editCourseDto.getCourseTime());
        course.setCourseCredit(editCourseDto.getCourseCredit());
        course.setCourseMax(editCourseDto.getCourseMax());
        courseDao.save(course);
        return "修改成功！";
    }
    /**
     * 添加一门选课
     */
    public String addCourse(AddCourseDto addCourseDto){
        Course course = new Course();
        course.setCourseName(addCourseDto.getCourseName());
        course.setCourseTime(addCourseDto.getCourseTime());
        course.setCourseCredit(addCourseDto.getCourseCredit());
        course.setCourseMax(addCourseDto.getCourseMax());
        courseDao.save(course);
        return "添加成功！";
    }
    /**
     * 选课
     */
    public String enroll(int courseId,int userId){
        int max = courseDao.findCourseByCourseId(courseId).getCourseMax();
        int time = courseDao.findCourseByCourseId(courseId).getCourseTime();
        int totalCount = userCourseDao.countByCourseIdAndStatus(courseId,0);
        int enrollCount = userCourseDao.countByUserIdAndStatus(userId,0);

        if (enrollCount >= 2){  //校验学生是否已报满两门
            return "您已报满两门课，无需再报！";
        } else if (enrollCount == 1){  //如果学生报了一门，校验学生是否已报此课程时段
            int myCourseId = userCourseDao.findUserCourseByUserIdAndStatus(userId,0).getCourseId();
            int myTime = courseDao.findCourseByCourseId(myCourseId).getCourseTime();
            if (myTime == time)
                return "不可重复报同一时段！";
        }
        if (totalCount >= max){ //检查课程人数是否已满
            return "已满，请重新选取！";
        } else {
            UserCourse userCourse = new UserCourse();
            userCourse.setCourseId(courseId);
            userCourse.setUserId(userId);
            userCourse.setStatus(0);
            userCourseDao.save(userCourse);
            return "报名成功！";
        }
    }
    /**
     * 结课
     */
    @Transactional
    public void finishClass(Integer courseId){
        userCourseDao.finishClass(courseId);
    }
}
